package com.wl.lib.extension

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.annotation.Px
import androidx.core.view.marginBottom
import androidx.core.view.marginEnd
import androidx.core.view.marginStart
import androidx.core.view.marginTop
import androidx.core.view.updateLayoutParams
import androidx.core.view.updateMarginsRelative
import com.sxu.shadowdrawable.ShadowDrawable

// Created by wangliang on 2024/12/4

@JvmOverloads
fun View.updateMargins(
    @Px start: Int = marginStart,
    @Px top: Int = marginTop,
    @Px end: Int = marginEnd,
    @Px bottom: Int = marginBottom
) {
    updateLayoutParams {
        (this as? ViewGroup.MarginLayoutParams)?.updateMarginsRelative(start, top, end, bottom)
    }
}

fun View.updateMarginTop(
    @Px top: Int = marginTop,
) {
    updateLayoutParams {
        (this as? ViewGroup.MarginLayoutParams)?.topMargin = top
    }
}

fun View.updateMarginStart(
    @Px start: Int = marginStart,
) {
    updateLayoutParams {
        (this as? ViewGroup.MarginLayoutParams)?.marginStart = start
    }
}

fun View.updateMarginEnd(
    @Px end: Int = marginEnd,
) {
    updateLayoutParams {
        (this as? ViewGroup.MarginLayoutParams)?.marginEnd = end
    }
}

fun View.addShadow(
    shadowWidth: Int = 8.dp,
    backgroundColor: Int = Color.WHITE,
    shadowRadius: Int = 8.dp,
    shadowColor: Int = Color.parseColor("#20000000"),
    offsetX: Int = 0,
    offsetY: Int = 0,
) {
    setPadding(shadowWidth, shadowWidth, shadowWidth, shadowWidth)
    ShadowDrawable.setShadowDrawable(this, backgroundColor, shadowRadius, shadowColor, shadowWidth, offsetX, offsetY)
}